<template>
  <el-breadcrumb class="app-levelbar" separator="/">
    <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
      <span class="no-redirect">{{item.name}}</span>
      <!--
      <span v-if='item.redirect==="noredirect" || index==levelList.length-1' class="no-redirect">{{item.name}}</span>
      <router-link v-else :to="item.redirect || item.path">{{item.name}}</router-link>
      -->
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script>
export default {
  created() {
    this.getBreadcrumb();
  },
  data() {
    return {
      levelList: null
    }
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.map(function(item){
        return {name: item.name, path: item.path};
      });
      const first = matched[0];
      if (first && (first.name !== '首页' || first.path !== '')) {
        //给最后一个path加上参数值
        if(JSON.stringify(this.$route.params) != '{}'){
          let pathSplit = this.$route.path.split('/');
          let paramValue = pathSplit[pathSplit.length-1];
          matched[matched.length-1].name = `${matched[matched.length-1].name}[${paramValue}]`;
        }
        matched = [{ name: '首页', path: '/' }].concat(matched);
      }
      this.levelList = matched;
    }
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-levelbar.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
  }
</style>
